package com.xms.consumers.security;

import com.xms.schema.event.EntityMetaCreatedEvent;
import com.xms.security.service.dataauth.IEntityPermissionCreaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 实体创建事件消费者之创建默认权限
 * </p>
 *
 * @author migo
 * @since 2021/10/8
 */
@Service
public class CreatePermissionsOnEntityCreated {
    private IEntityPermissionCreaterService _entityPermissionCreaterService;

    @Autowired
    public CreatePermissionsOnEntityCreated(IEntityPermissionCreaterService entityPermissionCreaterService) {
        _entityPermissionCreaterService = entityPermissionCreaterService;
    }

    @Async
    @EventListener
    public void handleEvent(EntityMetaCreatedEvent eventMessage) {
        try {
            _entityPermissionCreaterService.createDefaultPermissions(eventMessage.source);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
